/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.params;

import org.open.cobertura.model.AgentOption;
import org.open.cobertura.model.OptionEnum;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class AbstractAgentBuilder {
    public abstract void parseAgentArgs(String agentArgs);
    protected abstract void parseExcludesRule(String optionValue);
    protected abstract void parseIncludesRule(String optionValue);
    protected abstract void parsePort(String optionValue);



    /**
     * 判断类是否需要包含
     * @param className
     * @return
     */
    public boolean isInclude(String className){
        for(AgentOption agentOption:getIncludes()){
            if(matcher(className,agentOption)){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断类是否需要排除
     * @param className
     * @return
     */
    public boolean isExclude(String className){
        for(AgentOption agentOption:getExcludes()){
            if(matcher(className,agentOption)){
                return true;
            }
        }
        return false;
    }
    /**
     * 得到包含的类
     * @return
     */
    public abstract List<AgentOption> getIncludes();

    /**
     * 得到排除的类
     * @return
     */
    public abstract List<AgentOption> getExcludes();

    protected void parseIncludesRule(String[] options){
        if(options[0].equals(OptionEnum.INCLUDES.getName())){
            parseIncludesRule(options[1]);
        }
    }
    protected void parseExcludesRule(String[] options){
        if(options[0].equals(OptionEnum.EXCLUDES.getName())){
            parseExcludesRule(options[1]);
        }
    }

    protected void parsePort(String[] options){
        if(options[0].equals(OptionEnum.PORT.getName())){
            parsePort(options[1]);
        }
    }


    /**
     * 校验参数是否合法
     * @param optionName
     */
    protected void validation(String optionName){
        if(!optionName.equals(OptionEnum.INCLUDES.getName()) &&
            !optionName.equals(OptionEnum.EXCLUDES.getName()) &&
                !optionName.equals(OptionEnum.OUTPUT.getName()) &&
                !optionName.equals(OptionEnum.PORT.getName())
        ){
            throw new IllegalArgumentException("Illegal agent startup parameters, please refer to the OpenCobertura official website instructions");
        }
    }

    private boolean matcher(String className, AgentOption agentOption){
        String patternValue = className.replaceAll("/",".");
        Pattern pattern = Pattern.compile(agentOption.getValue());
        Matcher matcher = pattern.matcher(patternValue);
        return matcher.find();
    }
}
